Bash scripting is the method of automating bash/terminal commands.
lets understand bash scripting by building a ping sweeper:
usually when we run ping command ping 192.168.x.x it gives this output, by sending icmp packet.
if the system receives icmp packet back it means the other system is alive/connected to the same network.
PING 192.168.3.2 (192.168.3.2) 56(84) bytes of data.
64 bytes from 192.168.3.2: icmp_seq=1 ttl=128 time=0.929 ms
^C
--- 192.168.3.2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.929/0.929/0.929/0.000 ms
as u can see down below this is bash script that check if the system is alive by sending icmp packet
#!/bin/bashif["$1"==""];then# if $1 == null --> this passes anrgument and arguments are written by the userecho"Type the IP Address!"echo"This is the syntax: ./filename.sh 192.168.1"else# if $1 is not null it will execute the ping command foripin`seq1254`;doping-c1$1.$ip|grep"64 bytes"|cut-d" "-f4|tr-d":"&donefi# ending the statement
bash scripting is about automating bash/terminal commands as a single script.
#!/bin/bash the shebang, this tells the interpreter to execute as a bash file .sh
like in any programming Language we can use if/else statement to preform conditions.
In the else we made a for loop that check the IP subnet seq 1 254 of /24 prefix 192.168.1.x
by using pipe | we combine the ping commands and formatting methods and filtering this output:
64 bytes from 192.168.3.2: icmp_seq=1 ttl=128 time=0.929 ms
grep "64 bytes" this grabs the line as shown above
then we use cut -d "space" -f 4 to cut the delimiter till it finds the ipv4 and filter the ping response. this what remains after cutting 192.168.2.2: then we use tr -d ":" = translate to remove the ":".
then we are left we a clean ipv4 address example `192.168.1.1
Now the output of the for loop will give u the IP's of alive system on your network
run this commands to execute the code.
$1 is a paremeter will be taken by You who is running the script its like providing input in python
sudochmod +x # assuming that the file is not executable.
./pingsweeper 192.168.1 # the $1 value --> specify your ip subnet.
Creating a Bash script for setting up my kali VM After installing:
echo"hey,what's Your "read name
echo"welcome,$name"
Loops:
foriin{1..10};doecho$idone
Conditional Statements in Bash + Summary:
echo"Hey What is your Name"read name
echo"Welcome,$name"echo"Pleae enter Your name Alias:"read name2
if["$name2"="meezok"];thenecho"Welcome $name : Here is Temp login Password : IloveYou"elseecho"Sorry! You are not Authorized To Access"ip a
fiforiin{1..10};doecho$idone
Script Challenge:(Safe Locker ):
#!/bin/bashecho"-----ENTER YOUR CREDITENTAILS To Unlock THE VAULT-----"username=""company=""pin=""foriin{1..3};do# Defining the conditional statementsif["$i"-eq1];thenecho"Enter your Username:"read username
elif["$i"-eq2];thenecho"Enter your Company name:"read companyname
elseecho"Enter your PIN:"read pin
fidone# Checking if the user entered the correct detailsif["$username"="meezok"]&&["$company"="utas"]&&["$pin"="4321"];thenecho"Authentication Successful. You can now access your locker, John."elseecho"Authentication Denied!!"fi